#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ----------------------------------------------------------
# -- This's Client Main Frame Window
# -- 客户端主界面
# ****************************
# Author: lmaye.Zhou
# Blog: www.lmay.com
# Date: 2018年2月2日 15:57:01
# ----------------------------------------------------------
import datetime
import resource
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QVBoxLayout, QHBoxLayout, QGroupBox, QFormLayout, QWidget, QGridLayout, QPushButton, \
    QListWidget, QLabel, QFileDialog, QLineEdit
from core.handler.sync_pdf_handler import SyncGeneratePdfThread


class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.initQMainWindow()

    def initQMainWindow(self):
        """
            初始化界面

            :return:
        """
        self.creatFormGroupBox()
        self.createExcelGridGroupBox()
        main_layout = QVBoxLayout()
        hbox_layout = QHBoxLayout()
        hbox_layout.addWidget(self.grid_group_excel)
        main_layout.addWidget(self.form_group_box)
        main_layout.addLayout(hbox_layout)
        self.setLayout(main_layout)
        self.setFixedSize(640, 520)
        self.setWindowTitle("PDF文件转为图片")
        self.setWindowIcon(QIcon(":/favicon.png"))

    def creatFormGroupBox(self):
        """
            UI 日志输出区域

            :return:
        """
        self.form_group_box = QGroupBox("输出区域")
        layout = QFormLayout()
        self.text_edit = QListWidget()
        self.text_edit.setFixedSize(600, 400)
        layout.addRow(self.text_edit)
        self.form_group_box.setLayout(layout)

    def createExcelGridGroupBox(self):
        """
            PDF 操作区域

            :return:
        """
        self.grid_group_excel = QGroupBox("PDF 操作区域")
        layout = QGridLayout()

        label = QLabel("选择文件:")

        self.file_edit = QLineEdit()
        self.file_edit.setReadOnly(True)
        self.file_edit.setPlaceholderText("请选择文件")

        self.img_type = QLineEdit()
        self.img_type.setPlaceholderText("图片格式eg: jpg/png ..")

        btn_change = QPushButton("选择PDF")
        btn_change.clicked.connect(self.changeFile)

        btn_excel = QPushButton("生成图片")
        btn_excel.clicked.connect(self.execute_excel)
        layout.addWidget(label, 0, 0)
        layout.addWidget(self.file_edit, 0, 1)
        layout.addWidget(btn_change, 0, 2)
        layout.addWidget(self.img_type, 0, 3)
        layout.addWidget(btn_excel, 0, 4)
        layout.setColumnStretch(5, 1)
        self.grid_group_excel.setLayout(layout)

    def changeFile(self):
        """
            文件选择器

            :return:
        """
        file_name, file_type = QFileDialog.getOpenFileName(self, "选取文件", "C:/", "PDF Files (*.pdf)")
        if not file_name:
            return
        self.printMsg("{} 选中文件: {}".format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), file_name))
        if file_type[file_type.rindex(".") + 1: file_type.rindex(")")].lower() != "pdf":
            self.printMsg("{} 请选择PDF文件".format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
            return
        self.file_edit.setText(file_name)

    def printMsg(self, log):
        """
            界面打印日志

            :param log: 日志
            :return:
        """
        self.text_edit.addItem(log)
        # 自动滚动
        self.text_edit.setCurrentRow(self.text_edit.count() - 1)

    def execute_excel(self):
        """
            生成excel处理处理信号槽

            :return:
        """
        file_path = self.file_edit.text().strip()
        file_type = self.img_type.text().strip()
        if not file_path:
            self.printMsg("{} 请选择要转换的PDF文件...".format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
            return
        self.t3 = SyncGeneratePdfThread()
        self.t3.set_param(file_path, file_type)
        self.t3.signal_out.connect(self.printMsg)
        self.t3.start()
